package com.gusto.engine.clusterant.access.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.gusto.engine.clusterant.access.ClusterDAO;
import com.gusto.engine.clusterant.algorithms.kmeansclustering.Cluster;

/**
 * <p>Database {@link ClusterDAO} implementation.</p>
 * 
 * @author amokrane.belloui@gmail.com
 *
 */
public class ClusterDAOImpl implements ClusterDAO {
	
	String TABLE_CLUSTER = "clusterant_clusters";
	String COLUMN_COL = "col";
	String COLUMN_ROW = "row";
	String COLUMN_OBJECT = "object";
	String COLUMN_PLAN_ID = "plan_id";
	String COLUMN_CLUSTER_ID = "cluster_id";
	
	private int CONTEXT;
	public void setCONTEXT(int context) {
		CONTEXT = context;
	}
	
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	  
	public void removeAllObjects() {
		String remove = 
			"delete from " + TABLE_CLUSTER + " " + 
			"where " + COLUMN_PLAN_ID + " = ? ";
		
		Object[] params = new Object[] { CONTEXT };
		int[] types = new int[] { Types.INTEGER };
		
		jdbcTemplate.update(remove, params, types);
	}
	
	public void addObjectToCluster(int cluster, int row, int col, Object object) {
		String insert = 
			"insert into " + TABLE_CLUSTER + " " + 
			"(" + COLUMN_PLAN_ID + ", " + COLUMN_CLUSTER_ID + ", " + COLUMN_COL + ", " + COLUMN_ROW + ", " + COLUMN_OBJECT + ") " +
			"values (?, ?, ?, ?, ?)";
		
		Object[] params = new Object[] { CONTEXT, cluster, col, row, object };
		int[] types = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR };
		
		jdbcTemplate.update(insert, params, types);
	}
	
	public List<Cluster> getClusters(int cluster, int row, int col, Object object) {
		List<Cluster> clusters = new ArrayList<Cluster>();
		// FIXME Implement getClusters
		return clusters;
	}
	
	public String getTABLE_CLUSTER() {
		return TABLE_CLUSTER;
	}
	
	public void setTABLE_CLUSTER(String table_cluster) {
		TABLE_CLUSTER = table_cluster;
	}
	
	public String getCOLUMN_COL() {
		return COLUMN_COL;
	}

	public void setCOLUMN_COL(String column_col) {
		COLUMN_COL = column_col;
	}

	public String getCOLUMN_ROW() {
		return COLUMN_ROW;
	}

	public void setCOLUMN_ROW(String column_row) {
		COLUMN_ROW = column_row;
	}

	public String getCOLUMN_OBJECT() {
		return COLUMN_OBJECT;
	}

	public void setCOLUMN_OBJECT(String column_object) {
		COLUMN_OBJECT = column_object;
	}

	public String getCOLUMN_PLAN_ID() {
		return COLUMN_PLAN_ID;
	}

	public void setCOLUMN_PLAN_ID(String column_plan_id) {
		COLUMN_PLAN_ID = column_plan_id;
	}

	public String getCOLUMN_CLUSTER_ID() {
		return COLUMN_CLUSTER_ID;
	}

	public void setCOLUMN_CLUSTER_ID(String column_cluster_id) {
		COLUMN_CLUSTER_ID = column_cluster_id;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
}
